home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / c / eval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  25.8 KB  |  1,350 lines

  1. /*
  2.  Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
  3.  
  4. This file is part of GNU Common Lisp, herein referred to as GCL
  5.  
  6. GCL is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GCL is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
  14. License for more details.
  15.  
  16. You should have received a copy of the GNU Library General Public License 
  17. along with GCL; see the file COPYING.  If not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. */
  21.  
  22. /*
  23.     eval.c
  24. */
  25.  
  26. #include "include.h"
  27. #include "sfun_argd.h"
  28.  
  29. struct nil3 { object nil3_self[3]; } three_nils;
  30.  
  31. #ifdef DEBUG_AVMA
  32. #undef DEBUG_AVMA
  33. unsigned long avma,bot;
  34. #define DEBUG_AVMA unsigned long saved_avma =  avma;
  35. warn_avma()
  36.   print(list(2,make_simple_string("avma changed"),ihs_top_function_name()),
  37.     Vstandard_output->s.s_dbind);
  38. }
  39. #define CHECK_AVMA if(avma!= saved_avma) warn_avma();
  40. #define DEBUGGING_AVMA  
  41. #else
  42. #define DEBUG_AVMA
  43. #define CHECK_AVMA
  44. #endif
  45.  
  46. #undef endp
  47.  
  48. #define    endp(obje)    ((enum type)((endp_temp = (obje))->d.t) == t_cons ? \
  49.              FALSE : endp_temp == Cnil ? TRUE : \
  50.              (bool)FEwrong_type_argument(Slist, endp_temp))
  51.  
  52. object endp_temp;
  53.  
  54. int eval1 = 0;
  55. object c_apply_n();
  56.  
  57. object siSbreak_points;
  58. object siSbreak_step;
  59.  
  60.  
  61. #define SET_TO_APPLY(res,f,n,x) \
  62.  switch(n) {\
  63.  case 0:   res=((f))(); break;\
  64.   case 1:  res=((f))(x[0]); break; \
  65.   case 2:  res=((f))(x[0],x[1]);break; \
  66.   case 3:  res=((f))(x[0],x[1],x[2]);break; \
  67.   case 4:  res=((f))(x[0],x[1],x[2],x[3]);break; \
  68.   case 5:  res=((f))(x[0],x[1],x[2],x[3],x[4]);break; \
  69.   case 6:  res=((f))(x[0],x[1],x[2],x[3],x[4],x[5]);  break;\
  70.   case 7:  res=((f))(x[0],x[1],x[2],x[3],x[4],x[5], x[6]); break;\
  71.   case 8:  res=((f))(x[0],x[1],x[2],x[3],x[4],x[5], x[6],x[7]); break;\
  72.   case 9:  res=((f))(x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8]);break;\
  73.   case 10: res=((f))(x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9]);break;\
  74.    default: res=c_apply_n(f,n,x); break;}
  75.  
  76. /*
  77. #undef SET_TO_APPLY
  78. #define SET_TO_APPLY(res,f,n,x)  res=c_apply_n(f,n,x);
  79. */
  80.  
  81. /* for t_sfun,t_gfun with args on vs stack */
  82.  
  83. quick_call_sfun(fun)
  84.      object fun;
  85. { DEBUG_AVMA
  86.   int i,n;
  87.   enum ftype restype;
  88.   object *x,res;
  89.   object temp_ar[10];
  90.   i=fun->sfn.sfn_argd;
  91.   n=SFUN_NARGS(i);
  92.   if (n != vs_top -vs_base)
  93.     {check_arg_failed(n);}
  94.   restype = SFUN_RETURN_TYPE(i);
  95.   SFUN_START_ARG_TYPES(i);
  96.   /* for moment just support object and int */
  97. #define COERCE_ARG(a,type)  (type==f_object ? a : (object)(fix(a)))
  98.   if (i==0)
  99.     x=vs_base;
  100.   else
  101.     {int j;
  102.      x=temp_ar;
  103.      for (j=0; j<n ; j++)
  104.        {enum ftype typ=SFUN_NEXT_TYPE(i);
  105.     x[j]=COERCE_ARG(vs_base[j],typ);}}
  106.   SET_TO_APPLY(res,(object (*)())fun->sfn.sfn_self,n,x);
  107.   vs_base[0]=
  108.     (restype==f_object ?  res :
  109.      restype==f_fixnum ? make_fixnum((int)res)
  110.      :(object) FEerror("Bad result type"));
  111.   vs_top=vs_base+1;
  112.   CHECK_AVMA;
  113.   return;}
  114.  
  115. /* only for sfun not gfun !!  Does not check number of args */
  116. call_sfun_no_check(fun)
  117.      object fun;
  118. { DEBUG_AVMA
  119.   int n;
  120.   object *base=vs_base;
  121.   n=vs_top - base;
  122.   SET_TO_APPLY(base[0],(object (*)())fun->sfn.sfn_self,n,base);
  123.   vs_top=(vs_base=base)+1;
  124.   CHECK_AVMA;
  125.   return;
  126. }
  127. call_vfun(fun)
  128.      object fun;
  129. { DEBUG_AVMA
  130.   int n;
  131.   object *base=vs_base;
  132.   n=vs_top - base;
  133.   if (n < fun->vfn.vfn_minargs)
  134.     {FEtoo_few_arguments(base,vs_top); return;}
  135.   if (n > fun->vfn.vfn_maxargs)
  136.     {FEtoo_many_arguments(base,vs_top); return;}
  137.   VFUN_NARGS = n;
  138.   SET_TO_APPLY(base[0],(object (*)())fun->sfn.sfn_self,n,base);
  139.   vs_top=(vs_base=base)+1;
  140.   CHECK_AVMA;
  141.   return;
  142. }
  143.  
  144. static object temporary;
  145.  
  146. funcall(fun)
  147. object fun;
  148. {
  149.     object x;
  150.      object * VOL top;
  151.     object *lex;
  152.     bds_ptr old_bds_top;
  153.     VOL bool b;
  154.     bool c;
  155.     DEBUG_AVMA
  156.       TOP:
  157.     if (fun == OBJNULL)
  158.         FEerror("Undefined function.", 0);
  159.     switch (type_of(fun)) {
  160.     case t_cfun:
  161.         MMcall(fun);
  162.         CHECK_AVMA; return;
  163.     case t_gfun:    
  164.     case t_sfun:
  165.         ihs_check;ihs_push(fun);
  166.         quick_call_sfun(fun);
  167.         ihs_pop();
  168.         return;
  169.         case t_vfun:
  170.         ihs_check;ihs_push(fun);
  171.         call_vfun(fun);
  172.         ihs_pop();
  173.         return;
  174.     case t_cclosure:
  175.     {
  176.         object *top, *base, l;
  177.  
  178.         if (fun->cc.cc_turbo != NULL) {
  179.             MMccall(fun, fun->cc.cc_turbo);
  180.             CHECK_AVMA; return;
  181.         }
  182.         top = vs_top;
  183.         base = vs_base;
  184.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  185.             vs_push(l);
  186.         vs_base = vs_top;
  187.         while (base < top)
  188.             vs_push(*base++);
  189.         MMccall(fun, top);
  190.         CHECK_AVMA; return;
  191.     }
  192.         
  193.         case t_dclosure:
  194.         (*(fun)->dc.dc_self)(fun->dc.dc_env);
  195.         CHECK_AVMA; return;
  196.     case t_symbol:
  197.          {object x = fun->s.s_gfdef;
  198.           if (x) { fun = x; goto TOP;}
  199.           else
  200.         FEundefined_function(fun);
  201.           }
  202.  
  203.     case t_cons:
  204.         break;
  205.  
  206.     default:
  207.         FEinvalid_function(fun);
  208.     }
  209.  
  210.     /*
  211.         This part is the same as that of funcall_no_event.
  212.     */
  213.  
  214.     /* we may have pushed the calling form if this is called invoked from 
  215.        eval.   A lambda call requires vs_push's, so we can tell
  216.        if we pushed by vs_base being the same.
  217.        */
  218.       { VOL int not_pushed = 0;
  219.     if (vs_base !=     ihs_top->ihs_base){
  220.       ihs_check;
  221.       ihs_push(fun);
  222.     }
  223.     else
  224.       not_pushed = 1;
  225.  
  226.     ihs_top->ihs_base = lex_env;
  227.     x = MMcar(fun);
  228.     top = vs_top;
  229.     lex = lex_env;
  230.     old_bds_top = bds_top;
  231.  
  232.     /* maybe digest this lambda expression
  233.        (lambda-block-expand name ..) has already been
  234.        expanded.    The value of lambda-block-expand may
  235.        be a compiled function in which case we say expand
  236.        with it)
  237.      */
  238.  
  239.     if (x == siSlambda_block_expanded) {
  240.  
  241.       b = TRUE;
  242.       c = FALSE;
  243.       fun = fun->c.c_cdr;
  244.  
  245.     }else if (x == Slambda_block) {
  246.       b = TRUE;
  247.       c = FALSE;
  248.       if(type_of(siSlambda_block_expanded->s.s_dbind) == t_sfun)
  249.         fun = ifuncall1(siSlambda_block_expanded->s.s_dbind,fun);
  250.  
  251.       fun = fun->c.c_cdr;
  252.  
  253.  
  254.     
  255.     } else if (x == Slambda_closure) {
  256.         b = FALSE;
  257.         c = TRUE;
  258.         fun = fun->c.c_cdr;
  259.     } else if (x == Slambda) {
  260.         b = c = FALSE;
  261.         fun = fun->c.c_cdr;
  262.     } else if (x == Slambda_block_closure) {
  263.         b = c = TRUE;
  264.         fun = fun->c.c_cdr;
  265.     } else
  266.         b = c = TRUE;
  267.     if (c) {
  268.         vs_push(kar(fun));
  269.         fun = fun->c.c_cdr;
  270.         vs_push(kar(fun));
  271.         fun = fun->c.c_cdr;
  272.         vs_push(kar(fun));
  273.         fun = fun->c.c_cdr;
  274.     } else {
  275.         *(struct nil3 *)vs_top = three_nils;
  276.         vs_top += 3;
  277.     }
  278.     if (b) {
  279.         x = kar(fun);  /* block name */
  280.         fun = fun->c.c_cdr;
  281.     }
  282.     lex_env = top;
  283.     vs_push(fun);
  284.         lambda_bind(top);
  285.     ihs_top->ihs_base = lex_env;
  286.     if (b) {
  287.         fun = temporary = alloc_frame_id();
  288.         /*  lex_block_bind(x, temporary);  */
  289.         temporary = MMcons(temporary, Cnil);
  290.         temporary = MMcons(Sblock, temporary);
  291.         temporary = MMcons(x, temporary);
  292.         lex_env[2] = MMcons(temporary, lex_env[2]);
  293.         frs_push(FRS_CATCH, fun);
  294.         if (nlj_active) {
  295.             nlj_active = FALSE;
  296.             goto END;
  297.         }
  298.     }
  299.     x = top[3];  /* body */
  300.     if(endp(x)) {
  301.         vs_base = vs_top;
  302.         vs_push(Cnil);
  303.     } else {
  304.         top = vs_top;
  305.         for (;;) {
  306.             eval(MMcar(x));
  307.             x = MMcdr(x);
  308.             if (endp(x))
  309.                 break;
  310.             vs_top = top;
  311.         }
  312.     }
  313. END:
  314.     if (b)
  315.         frs_pop();
  316.     bds_unwind(old_bds_top);
  317.     lex_env = lex;
  318.     if (not_pushed == 0) {ihs_pop();}
  319.     CHECK_AVMA;
  320. }}
  321.  
  322.  
  323. funcall_no_event(fun)
  324. object fun;
  325. { DEBUG_AVMA
  326.     if (fun == OBJNULL)
  327.         FEerror("Undefined function.", 0);
  328.     switch (type_of(fun)) {
  329.     case t_cfun:
  330.         (*fun->cf.cf_self)();
  331.         break;
  332.  
  333.     case t_cclosure:
  334.     {
  335.         object *top, *base, l;
  336.  
  337.         if (fun->cc.cc_turbo != NULL) {
  338.             (*fun->cc.cc_self)(fun->cc.cc_turbo);
  339.             break;
  340.         }
  341.         top = vs_top;
  342.         base = vs_base;
  343.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  344.             vs_push(l);
  345.         vs_base = vs_top;
  346.         while (base < top)
  347.             vs_push(*base++);
  348.         (*fun->cc.cc_self)(top);
  349.         break;
  350.     }
  351.  
  352.     case t_sfun:
  353.         call_sfun_no_check(fun); return;
  354.         case t_gfun:
  355.         quick_call_sfun(fun); return;
  356.         case t_vfun:
  357.         call_vfun(fun); return;
  358.         case t_dclosure:
  359.         (*(fun)->dc.dc_self)(fun->dc.dc_env);
  360.         CHECK_AVMA; return;
  361.  
  362.     default:
  363.         funcall(fun);
  364.         
  365.     }
  366. }
  367.  
  368. lispcall(funp, narg)
  369. object *funp;
  370. int narg;
  371. { DEBUG_AVMA
  372.     object fun = *funp;
  373.  
  374.     vs_base = funp + 1;
  375.     vs_top = vs_base + narg;
  376.  
  377.     if (fun == OBJNULL)
  378.         FEerror("Undefined function.", 0);
  379.     switch (type_of(fun)) {
  380.     case t_cfun:
  381.         MMcall(fun);
  382.         break;
  383.  
  384.     case t_cclosure:
  385.     {
  386.         object *top, *base, l;
  387.  
  388.         if (fun->cc.cc_turbo != NULL) {
  389.             MMccall(fun, fun->cc.cc_turbo);
  390.             break;
  391.         }
  392.         top = vs_top;
  393.         base = vs_base;
  394.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  395.             vs_push(l);
  396.         vs_base = vs_top;
  397.         while (base < top)
  398.             vs_push(*base++);
  399.         MMccall(fun, top);
  400.         break;
  401.     }
  402.  
  403.           default:
  404.         funcall(fun);
  405.  
  406.     }
  407.   CHECK_AVMA;
  408. }
  409.  
  410. lispcall_no_event(funp, narg)
  411. object *funp;
  412. int narg;
  413. {        DEBUG_AVMA
  414.     object fun = *funp;
  415.  
  416.     vs_base = funp + 1;
  417.     vs_top = vs_base + narg;
  418.  
  419.     if (fun == OBJNULL)
  420.         FEerror("Undefined function.", 0);
  421.     switch (type_of(fun)) {
  422.     case t_cfun:
  423.         (*fun->cf.cf_self)();
  424.         break;
  425.  
  426.     case t_cclosure:
  427.     {
  428.         object *top, *base, l;
  429.  
  430.         if (fun->cc.cc_turbo != NULL) {
  431.             (*fun->cc.cc_self)(fun->cc.cc_turbo);
  432.             break;
  433.         }
  434.         top = vs_top;
  435.         base = vs_base;
  436.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  437.             vs_push(l);
  438.         vs_base = vs_top;
  439.         while (base < top)
  440.             vs_push(*base++);
  441.         (*fun->cc.cc_self)(top);
  442.         break;
  443.     }
  444.  
  445.  
  446.     default:
  447.         funcall(fun);
  448.  
  449.     }
  450.      CHECK_AVMA;
  451. }
  452.  
  453. symlispcall(sym, base, narg)
  454. object sym, *base;
  455. int narg;
  456. {       DEBUG_AVMA
  457.     object fun = symbol_function(sym);
  458.  
  459.     vs_base = base;
  460.     vs_top = vs_base + narg;
  461.  
  462.     if (fun == OBJNULL)
  463.         FEerror("Undefined function.", 0);
  464.     switch (type_of(fun)) {
  465.     case t_cfun:
  466.         MMcall(fun);
  467.         break;
  468.  
  469.     case t_cclosure:
  470.     {
  471.         object *top, *base, l;
  472.  
  473.         if (fun->cc.cc_turbo != NULL) {
  474.             MMccall(fun, fun->cc.cc_turbo);
  475.             break;
  476.         }
  477.         top = vs_top;
  478.         base = vs_base;
  479.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  480.             vs_push(l);
  481.         vs_base = vs_top;
  482.         while (base < top)
  483.             vs_push(*base++);
  484.         MMccall(fun, top);
  485.         break;
  486.     }
  487.  
  488.     default:
  489.         funcall(fun);
  490.     }
  491.     CHECK_AVMA;
  492. }
  493.  
  494. symlispcall_no_event(sym, base, narg)
  495. object sym, *base;
  496. int narg;
  497. {       DEBUG_AVMA
  498.     object fun = symbol_function(sym);
  499.  
  500.     vs_base = base;
  501.     vs_top = vs_base + narg;
  502.  
  503.     if (fun == OBJNULL)
  504.         FEerror("Undefined function.", 0);
  505.     switch (type_of(fun)) {
  506.     case t_cfun:
  507.         (*fun->cf.cf_self)();
  508.         break;
  509.  
  510.     case t_cclosure:
  511.     {
  512.         object *top, *base, l;
  513.  
  514.         if (fun->cc.cc_turbo != NULL) {
  515.             (*fun->cc.cc_self)(fun->cc.cc_turbo);
  516.             break;
  517.         }
  518.         top = vs_top;
  519.         base = vs_base;
  520.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  521.             vs_push(l);
  522.         vs_base = vs_top;
  523.         while (base < top)
  524.             vs_push(*base++);
  525.         (*fun->cc.cc_self)(top);
  526.         break;
  527.     }
  528.  
  529.     default:
  530.         funcall(fun);
  531.  
  532.     }
  533.     CHECK_AVMA;
  534. }
  535.  
  536. object
  537. simple_lispcall(funp, narg)
  538. object *funp;
  539. int narg;
  540. {       DEBUG_AVMA
  541.     object fun = *funp;
  542.     object *sup = vs_top;
  543.  
  544.     vs_base = funp + 1;
  545.     vs_top = vs_base + narg;
  546.  
  547.     if (fun == OBJNULL)
  548.         FEerror("Undefined function.", 0);
  549.     switch (type_of(fun)) {
  550.     case t_cfun:
  551.         MMcall(fun);
  552.         break;
  553.  
  554.     case t_cclosure:
  555.     {
  556.         object *top, *base, l;
  557.  
  558.         if (fun->cc.cc_turbo != NULL) {
  559.             MMccall(fun, fun->cc.cc_turbo);
  560.             break;
  561.         }
  562.         top = vs_top;
  563.         base = vs_base;
  564.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  565.             vs_push(l);
  566.         vs_base = vs_top;
  567.         while (base < top)
  568.             vs_push(*base++);
  569.         MMccall(fun, top);
  570.         break;
  571.     }
  572.  
  573.     default:
  574.         funcall(fun);
  575.     }
  576.     vs_top = sup;
  577.     CHECK_AVMA;
  578.     return(vs_base[0]);
  579.     
  580. }
  581.  
  582. object
  583. simple_lispcall_no_event(funp, narg)
  584. object *funp;
  585. int narg;
  586. {       DEBUG_AVMA 
  587.     object fun = *funp;
  588.     object *sup = vs_top;
  589.  
  590.     vs_base = funp + 1;
  591.     vs_top = vs_base + narg;
  592.  
  593.     if (fun == OBJNULL)
  594.         FEerror("Undefined function.", 0);
  595.     switch (type_of(fun)) {
  596.     case t_cfun:
  597.         (*fun->cf.cf_self)();
  598.         break;
  599.  
  600.     case t_cclosure:
  601.     {
  602.         object *top, *base, l;
  603.  
  604.         if (fun->cc.cc_turbo != NULL) {
  605.             (*fun->cc.cc_self)(fun->cc.cc_turbo);
  606.             break;
  607.         }
  608.         top = vs_top;
  609.         base = vs_base;
  610.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  611.             vs_push(l);
  612.         vs_base = vs_top;
  613.         while (base < top)
  614.             vs_push(*base++);
  615.         (*fun->cc.cc_self)(top);
  616.         break;
  617.     }
  618.  
  619.     default:
  620.         funcall(fun);
  621.  
  622.     }
  623.     vs_top = sup;
  624.     CHECK_AVMA;
  625.     return(vs_base[0]);
  626. }
  627.  
  628. object
  629. simple_symlispcall(sym, base, narg)
  630. object sym, *base;
  631. int narg;
  632. {       DEBUG_AVMA
  633.     object fun = symbol_function(sym);
  634.     object *sup = vs_top;
  635.  
  636.     vs_base = base;
  637.     vs_top = vs_base + narg;
  638.  
  639.     if (fun == OBJNULL)
  640.         FEerror("Undefined function.", 0);
  641.     switch (type_of(fun)) {
  642.     case t_cfun:
  643.         MMcall(fun);
  644.         break;
  645.  
  646.     case t_cclosure:
  647.     {
  648.         object *top, *base, l;
  649.  
  650.         if (fun->cc.cc_turbo != NULL) {
  651.             MMccall(fun, fun->cc.cc_turbo);
  652.             break;
  653.         }
  654.         top = vs_top;
  655.         base = vs_base;
  656.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  657.             vs_push(l);
  658.         vs_base = vs_top;
  659.         while (base < top)
  660.             vs_push(*base++);
  661.         MMccall(fun, top);
  662.         break;
  663.     }
  664.  
  665.     default:
  666.         funcall(fun);
  667.  
  668.     }
  669.     vs_top = sup;
  670.     CHECK_AVMA;
  671.     return(vs_base[0]);
  672. }
  673.  
  674. object
  675. simple_symlispcall_no_event(sym, base, narg)
  676. object sym, *base;
  677. int narg;
  678. {       DEBUG_AVMA
  679.     object fun = symbol_function(sym);
  680.     object *sup = vs_top;
  681.  
  682.     vs_base = base;
  683.     vs_top = vs_base + narg;
  684.  
  685.     if (fun == OBJNULL)
  686.         FEerror("Undefined function.", 0);
  687.     switch (type_of(fun)) {
  688.     case t_cfun:
  689.         (*fun->cf.cf_self)();
  690.         break;
  691.  
  692.     case t_cclosure:
  693.     {
  694.         object *top, *base, l;
  695.  
  696.         if (fun->cc.cc_turbo != NULL) {
  697.             (*fun->cc.cc_self)(fun->cc.cc_turbo);
  698.             break;
  699.         }
  700.         top = vs_top;
  701.         base = vs_base;
  702.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  703.             vs_push(l);
  704.         vs_base = vs_top;
  705.         while (base < top)
  706.             vs_push(*base++);
  707.         (*fun->cc.cc_self)(top);
  708.         break;
  709.     }
  710.  
  711.     default:
  712.         funcall(fun);
  713.     }
  714.     vs_top = sup;
  715.     CHECK_AVMA;
  716.     return(vs_base[0]);
  717. }
  718.  
  719. super_funcall(fun)
  720. object fun;
  721. {
  722.     if (type_of(fun) == t_symbol) {
  723.         if (fun->s.s_sfdef != NOT_SPECIAL || fun->s.s_mflag)
  724.             FEinvalid_function(fun);
  725.         if (fun->s.s_gfdef == OBJNULL)
  726.             FEundefined_function(fun);
  727.         fun = fun->s.s_gfdef;
  728.     }
  729.     funcall(fun);
  730. }
  731.  
  732. super_funcall_no_event(fun)
  733. object fun;
  734. {
  735. #ifdef DEBUGGING_AVMA
  736.   funcall_no_event(fun); return;
  737. #endif 
  738.    if (type_of(fun)==t_cfun){(*fun->cf.cf_self)();return;}
  739.    if (type_of(fun)==t_sfun){call_sfun_no_check(fun); return;}
  740.    if (type_of(fun)==t_gfun)
  741.        {quick_call_sfun(fun); return;}
  742.    if (type_of(fun)==t_vfun)
  743.        {call_vfun(fun); return;}
  744.    if (type_of(fun) == t_symbol) {
  745.       if (fun->s.s_sfdef != NOT_SPECIAL || fun->s.s_mflag)
  746.             FEinvalid_function(fun);
  747.         if (fun->s.s_gfdef == OBJNULL)
  748.             FEundefined_function(fun);
  749.         fun = fun->s.s_gfdef;
  750.         if (type_of(fun)==t_cfun){(*fun->cf.cf_self)();
  751.                       return;}
  752.     }
  753.     funcall_no_event(fun);
  754. }
  755.  
  756. eval(form)
  757. object form;
  758. {       DEBUG_AVMA
  759.     object fun, x;
  760.     object *top;
  761.     object *base;
  762.     object orig_form;
  763.  
  764.     cs_check(form);
  765.  
  766. EVAL:
  767.  
  768.     vs_check;
  769.  
  770.     if (Vevalhook->s.s_dbind != Cnil && eval1 == 0)
  771.     {
  772.         bds_ptr old_bds_top = bds_top;
  773.         object hookfun = symbol_value(Vevalhook);
  774.         /*  check if Vevalhook is unbound  */
  775.  
  776.         bds_bind(Vevalhook, Cnil);
  777.         vs_base = vs_top;
  778.         vs_push(form);
  779.         vs_push(lex_env[0]);
  780.         vs_push(lex_env[1]);
  781.         vs_push(lex_env[2]);
  782.         vs_push(Cnil);
  783.         stack_cons();
  784.         stack_cons();
  785.         stack_cons();
  786.         super_funcall(hookfun);
  787.         bds_unwind(old_bds_top);
  788.         return;
  789.     } else
  790.         eval1 = 0;
  791.  
  792.     if (type_of(form) == t_cons)
  793.         goto APPLICATION;
  794.  
  795.     if (type_of(form) != t_symbol) {
  796.         vs_base = vs_top;
  797.         vs_push(form);
  798.         return;
  799.     }
  800.  
  801. SYMBOL:
  802.     switch (form->s.s_stype) {
  803.     case stp_constant:
  804.         vs_base = vs_top;
  805.         vs_push(form->s.s_dbind);
  806.         return;
  807.  
  808.     case stp_special:
  809.         if(form->s.s_dbind == OBJNULL)
  810.             FEunbound_variable(form);
  811.         vs_base = vs_top;
  812.         vs_push(form->s.s_dbind);
  813.         return;
  814.  
  815.     default:
  816.         /*  x = lex_var_sch(form);  */
  817.         for (x = lex_env[0];  type_of(x) == t_cons;  x = x->c.c_cdr)
  818.             if (x->c.c_car->c.c_car == form) {
  819.                 x = x->c.c_car->c.c_cdr;
  820.                 if (endp(x))
  821.                     break;
  822.                 vs_base = vs_top;
  823.                 vs_push(x->c.c_car);
  824.                 return;
  825.             }
  826.         if(form->s.s_dbind == OBJNULL)
  827.             FEunbound_variable(form);
  828.         vs_base = vs_top;
  829.         vs_push(form->s.s_dbind);
  830.         return;
  831.     }
  832.  
  833. APPLICATION:
  834.     /* Hook for possibly stopping at forms in the break point
  835.        list.  Also for stepping.  We only want to check
  836.        one form each time round, so we do *breakpoints*
  837.        */
  838.     if (siSbreak_points->s.s_dbind != Cnil)
  839.       { if (siSbreak_step->s.s_dbind == Cnil ||
  840.         ifuncall2(siSbreak_step->s.s_dbind,form,
  841.               list(3,lex_env[0],lex_env[1],lex_env[2])) == Cnil)
  842.           {object* bpts = siSbreak_points->s.s_dbind->v.v_self;
  843.            int i = siSbreak_points->s.s_dbind->v.v_fillp;
  844.            while (--i >= 0)
  845.          { if((*bpts)->c.c_car == form)
  846.              {ifuncall2(siSbreak_points->s.s_gfdef,form,
  847.                 list(3,lex_env[0],lex_env[1],lex_env[2]));
  848.  
  849.               break;}
  850.            bpts++;}
  851.          }}
  852.     
  853.     fun = MMcar(form);
  854.     if (type_of(fun) != t_symbol)
  855.         goto LAMBDA;
  856.     if (fun->s.s_sfdef != NOT_SPECIAL) {
  857.         ihs_check;
  858.         ihs_push(form);
  859.         ihs_top->ihs_base = lex_env;
  860.         (*fun->s.s_sfdef)(MMcdr(form));
  861.         CHECK_AVMA;
  862.         ihs_pop();
  863.         return;
  864.     }
  865.     /*  x = lex_fd_sch(fun);  */
  866.     for (x = lex_env[1];  type_of(x) == t_cons;  x = x->c.c_cdr)
  867.         if (x->c.c_car->c.c_car == fun) {
  868.             x = x->c.c_car;
  869.             if (MMcadr(x) == Smacro) {
  870.                 x = MMcaddr(x);
  871.                 goto EVAL_MACRO;
  872.             }
  873.             x = MMcaddr(x);
  874.             goto EVAL_ARGS;
  875.         }
  876.  
  877. GFDEF:
  878.     if ((x = fun->s.s_gfdef) == OBJNULL)
  879.         FEundefined_function(fun);
  880.  
  881.     if (fun->s.s_mflag) {
  882.     EVAL_MACRO:
  883.         top = vs_top;
  884.         macro_expand1(x, form);
  885.         form = vs_base[0];
  886.         vs_top = top;
  887.         vs_push(form);
  888.         goto EVAL;
  889.     }
  890.  
  891.       
  892.     
  893. EVAL_ARGS:
  894.     vs_push(x);
  895.     ihs_check;
  896.     ihs_push(form);
  897.     ihs_top->ihs_base = lex_env;
  898.     form = form->c.c_cdr;
  899.     base = vs_top;
  900.     top = vs_top;
  901.     while(!endp(form)) {
  902.         eval(MMcar(form));
  903.         top[0] = vs_base[0];
  904.         vs_top = ++top;
  905.         form = MMcdr(form);
  906.     }
  907.     vs_base = base;
  908.     if (Vapplyhook->s.s_dbind != Cnil) {
  909.         call_applyhook(fun);
  910.         return;
  911.     }
  912.     ihs_top->ihs_function = x;
  913.     ihs_top->ihs_base = vs_base;
  914.     if (type_of(x) == t_cfun) 
  915.       (*(x)->cf.cf_self)();
  916.     else
  917.       funcall_no_event(x);
  918.     CHECK_AVMA;
  919.     ihs_pop();
  920.     return;
  921.  
  922. LAMBDA:
  923.     if (type_of(fun) == t_cons && MMcar(fun) == Slambda) {
  924.         temporary = make_cons(lex_env[2], fun->c.c_cdr);
  925.         temporary = make_cons(lex_env[1], temporary);
  926.         temporary = make_cons(lex_env[0], temporary);
  927.         x = make_cons(Slambda_closure, temporary);
  928.         vs_push(x);
  929.         goto EVAL_ARGS;
  930.     }
  931.     FEinvalid_function(fun);
  932. }    
  933.  
  934. call_applyhook(fun)
  935. object fun;
  936. {
  937.     object ah;
  938.     object *v;
  939.  
  940.     ah = symbol_value(Vapplyhook);
  941.     v = vs_base + 1;
  942.     vs_push(Cnil);
  943.     while (vs_top > v)
  944.         stack_cons();
  945.     vs_push(vs_base[0]);
  946.     vs_base[0] = fun;
  947.     vs_push(lex_env[0]);
  948.     vs_push(lex_env[1]);
  949.     vs_push(lex_env[2]);
  950.     vs_push(Cnil);
  951.     stack_cons();
  952.     stack_cons();
  953.     stack_cons();
  954.     super_funcall(ah);
  955. }
  956.  
  957. Lfuncall()
  958. {
  959.     if (vs_top-vs_base < 1)
  960.         too_few_arguments();
  961.     vs_base++;
  962.     super_funcall(vs_base[-1]);
  963. }
  964.  
  965. Lapply()
  966. {
  967.     object lastarg;
  968.     if (vs_top-vs_base < 2)
  969.         too_few_arguments();
  970.     lastarg = vs_pop;
  971.     while (!endp(lastarg)) {
  972.         vs_push(MMcar(lastarg));
  973.         lastarg = MMcdr(lastarg);
  974.     }
  975.     vs_base++;
  976.     super_funcall(vs_base[-1]);
  977. }
  978.  
  979. Leval()
  980. {
  981.     object *lex = lex_env;
  982.  
  983.     check_arg(1);
  984.     lex_new();
  985.     eval(vs_base[0]);
  986.     lex_env = lex;
  987. }
  988.  
  989. Levalhook()
  990. {
  991.     object env;
  992.     bds_ptr old_bds_top = bds_top;
  993.     object *lex = lex_env;
  994.     int n = vs_top - vs_base;
  995.  
  996.     lex_env = vs_top;
  997.     if (n < 3)
  998.         too_few_arguments();
  999.     else if (n == 3) {
  1000.         *(struct nil3 *)vs_top = three_nils;
  1001.         vs_top += 3;
  1002.     } else if (n == 4) {
  1003.         env = vs_base[3];
  1004.         vs_push(car(env));
  1005.         env = cdr(env);
  1006.         vs_push(car(env));
  1007.         env = cdr(env);
  1008.         vs_push(car(env));
  1009.     } else
  1010.         too_many_arguments();
  1011.     bds_bind(Vevalhook, vs_base[1]);
  1012.     bds_bind(Vapplyhook, vs_base[2]);
  1013.     eval1 = 1;
  1014.     eval(vs_base[0]);
  1015.     lex_env = lex;
  1016.     bds_unwind(old_bds_top);
  1017. }
  1018.  
  1019. Lapplyhook()
  1020. {
  1021.     object env;
  1022.     bds_ptr old_bds_top = bds_top;
  1023.     object *lex = lex_env;
  1024.     int n = vs_top - vs_base;
  1025.     object l, *z;
  1026.  
  1027.     lex_env = vs_top;
  1028.     if (n < 4)
  1029.         too_few_arguments();
  1030.     else if (n == 4) {
  1031.         *(struct nil3 *)vs_top = three_nils;
  1032.         vs_top += 3;
  1033.     } else if (n == 5) {
  1034.         env = vs_base[4];
  1035.         vs_push(car(env));
  1036.         env = cdr(env);
  1037.         vs_push(car(env));
  1038.         env = cdr(env);
  1039.         vs_push(car(env));
  1040.     } else
  1041.         too_many_arguments();
  1042.     bds_bind(Vevalhook, vs_base[2]);
  1043.     bds_bind(Vapplyhook, vs_base[3]);
  1044.     z = vs_top;
  1045.     for (l = vs_base[1];  !endp(l);  l = l->c.c_cdr)
  1046.         vs_push(l->c.c_car);
  1047.     l = vs_base[0];
  1048.     vs_base = z;
  1049.     super_funcall(l);
  1050.     lex_env = lex;
  1051.     bds_unwind(old_bds_top);
  1052. }
  1053.  
  1054. Lconstantp()
  1055. {
  1056.     enum type x;
  1057.     check_arg(1);
  1058.  
  1059.     x = type_of(vs_base[0]);
  1060.     if(x == t_cons)
  1061.         if(vs_base[0]->c.c_car == Squote)
  1062.             vs_base[0] = Ct;
  1063.         else    vs_base[0] = Cnil;
  1064.     else if(x == t_symbol)
  1065.         if((enum stype)vs_base[0]->s.s_stype == stp_constant)
  1066.             vs_base[0] = Ct;
  1067.         else
  1068.             vs_base[0] = Cnil;
  1069.     else
  1070.             vs_base[0] = Ct;
  1071. }
  1072.  
  1073. object
  1074. ieval(x)
  1075. object x;
  1076. {
  1077.     object *old_vs_base;
  1078.     object *old_vs_top;
  1079.  
  1080.     old_vs_base = vs_base;
  1081.     old_vs_top = vs_top;
  1082.     eval(x);
  1083.     x = vs_base[0];
  1084.     vs_base = old_vs_base;
  1085.     vs_top = old_vs_top;
  1086.     return(x);
  1087. }
  1088.  
  1089. object
  1090. ifuncall1(fun, arg1)
  1091. object fun, arg1;
  1092. {
  1093.     object *old_vs_base;
  1094.     object *old_vs_top;
  1095.     object x;
  1096.  
  1097.     old_vs_base = vs_base;
  1098.     old_vs_top = vs_top;
  1099.     vs_base = vs_top;
  1100.     vs_push(arg1);
  1101.     super_funcall(fun);
  1102.     x = vs_base[0];
  1103.     vs_top = old_vs_top;
  1104.     vs_base = old_vs_base;
  1105.     return(x);
  1106. }
  1107.  
  1108. object
  1109. ifuncall2(fun, arg1, arg2)
  1110. object fun, arg1, arg2;
  1111. {
  1112.     object *old_vs_base;
  1113.     object *old_vs_top;
  1114.     object x;
  1115.  
  1116.     old_vs_base = vs_base;
  1117.     old_vs_top = vs_top;
  1118.     vs_base = vs_top;
  1119.     vs_push(arg1);
  1120.     vs_push(arg2);
  1121.     super_funcall(fun);
  1122.     x = vs_base[0];
  1123.     vs_top = old_vs_top;
  1124.     vs_base = old_vs_base;
  1125.     return(x);
  1126. }
  1127.  
  1128. object
  1129. ifuncall3(fun, arg1, arg2, arg3)
  1130. object fun, arg1, arg2, arg3;
  1131. {
  1132.     object *old_vs_base;
  1133.     object *old_vs_top;
  1134.     object x;
  1135.  
  1136.     old_vs_base = vs_base;
  1137.     old_vs_top = vs_top;
  1138.     vs_base = vs_top;
  1139.     vs_push(arg1);
  1140.     vs_push(arg2);
  1141.     vs_push(arg3);
  1142.     super_funcall(fun);
  1143.     x = vs_base[0];
  1144.     vs_top = old_vs_top;
  1145.     vs_base = old_vs_base;
  1146.     return(x);
  1147. }
  1148.  
  1149. funcall_with_catcher(fname, fun)
  1150. object fname, fun;
  1151. {
  1152.     int n = vs_top - vs_base;
  1153.     if (n > 64) n = 64;
  1154.     frs_push(FRS_CATCH, make_cons(fname, make_fixnum(n)));
  1155.     if (nlj_active)
  1156.         nlj_active = FALSE;
  1157.     else
  1158.         funcall(fun);
  1159.     frs_pop();
  1160. }
  1161.  
  1162. #include <varargs.h>
  1163.  
  1164. object 
  1165. fcalln_cclosure(ap)
  1166. va_list ap;
  1167. {int i=fcall.argd;
  1168.  {object *base=vs_top;
  1169.   DEBUG_AVMA
  1170.     vs_base=base;
  1171.     switch(i){
  1172.     case 10: *(base++)=va_arg(ap,object);
  1173.     case 9: *(base++)=va_arg(ap,object);
  1174.     case 8: *(base++)=va_arg(ap,object);
  1175.     case 7: *(base++)=va_arg(ap,object);
  1176.     case 6: *(base++)=va_arg(ap,object);
  1177.     case 5: *(base++)=va_arg(ap,object);
  1178.     case 4: *(base++)=va_arg(ap,object);
  1179.     case 3: *(base++)=va_arg(ap,object);
  1180.     case 2: *(base++)=va_arg(ap,object);
  1181.     case 1: *(base++)=va_arg(ap,object);
  1182.     case 0: break;
  1183.     default:
  1184.       FEerror(0,"bad args");
  1185.     } vs_top=base;
  1186.       base=base -i;
  1187.       do{object fun=fcall.fun;
  1188.         object *top, *base, l;
  1189.  
  1190.         if (fun->cc.cc_turbo != NULL) {
  1191.             (*fun->cc.cc_self)(fun->cc.cc_turbo);
  1192.             break;
  1193.         }
  1194.         top = vs_top;
  1195.         base = vs_base;
  1196.         for (l = fun->cc.cc_env;  !endp(l);  l = l->c.c_cdr)
  1197.             vs_push(l);
  1198.         vs_base = vs_top;
  1199.         while (base < top)
  1200.             vs_push(*base++);
  1201.         (*fcall.fun->cc.cc_self)(top);
  1202.         break;
  1203.     }while (0);
  1204.        vs_top=base;
  1205.        CHECK_AVMA;
  1206.        return(vs_base[0]);
  1207. }}
  1208.  
  1209. object 
  1210. fcalln_general(ap)
  1211. va_list ap;
  1212. {int i=fcall.argd;
  1213.  object fun=fcall.fun;
  1214.  {int n= SFUN_NARGS(i);
  1215.   /*  object *old_vs_base=vs_base; */
  1216.   object *old_vs_top=vs_top;
  1217.   object x;
  1218.   enum ftype typ,restype=SFUN_RETURN_TYPE(i);
  1219.   vs_top =  vs_base = old_vs_top;
  1220.   SFUN_START_ARG_TYPES(i);
  1221.   if (i==0)
  1222.     while (n-- > 0)
  1223.       { typ= SFUN_NEXT_TYPE(i);
  1224.     x =
  1225.       (typ==f_object ?    va_arg(ap,object):
  1226.        typ==f_fixnum ? make_fixnum(va_arg(ap,fixnum)):
  1227.        (object) FEerror("bad type",0));
  1228.     *(vs_top++) = x;}
  1229.   else
  1230.     {object *base=vs_top;
  1231.      while (n-- > 0)
  1232.        { *(base++) = va_arg(ap,object);}
  1233.      vs_top=base;}
  1234.   funcall(fcall.fun);
  1235.   x= vs_base[0];
  1236.   vs_top=old_vs_top;
  1237.   /* vs_base=old_vs_base; */
  1238.   return (restype== f_object ? x :
  1239.       restype== f_fixnum ? (object) (fix(x)):
  1240.       (object) FEerror("bad type",0));
  1241. }}
  1242. object
  1243. fcalln_vfun(vl)
  1244.   va_list vl;
  1245. {object *new,res;
  1246.  DEBUG_AVMA
  1247.  COERCE_VA_LIST(new,vl,fcall.argd);
  1248.  res = c_apply_n(fcall.fun->vfn.vfn_self,fcall.argd,new);
  1249.  CHECK_AVMA;
  1250.  return res; 
  1251. }
  1252.  
  1253. object 
  1254. fcalln(va_alist)
  1255. va_dcl
  1256. {  va_list ap;
  1257.    object fun=fcall.fun;
  1258.    DEBUG_AVMA
  1259.    va_start(ap);
  1260.    if(type_of(fun)==t_cfun)
  1261.      {object *base=vs_top;
  1262.       int i=fcall.argd;
  1263.       vs_base=base;
  1264.       switch(i){
  1265.       case 10: *(base++)=va_arg(ap,object);
  1266.       case 9: *(base++)=va_arg(ap,object);
  1267.       case 8: *(base++)=va_arg(ap,object);
  1268.       case 7: *(base++)=va_arg(ap,object);
  1269.       case 6: *(base++)=va_arg(ap,object);
  1270.       case 5: *(base++)=va_arg(ap,object);
  1271.       case 4: *(base++)=va_arg(ap,object);
  1272.       case 3: *(base++)=va_arg(ap,object);
  1273.       case 2: *(base++)=va_arg(ap,object);
  1274.       case 1: *(base++)=va_arg(ap,object);
  1275.       case 0: break;
  1276.       default:
  1277.     FEerror(0,"bad args");
  1278.       }  vs_top=base;
  1279.       base=base -i;
  1280.       (*fcall.fun->cf.cf_self)();
  1281.       vs_top=base;
  1282.       CHECK_AVMA;
  1283.       return(vs_base[0]);
  1284.     }
  1285.    if(type_of(fun)==t_cclosure)
  1286.      return(fcalln_cclosure(ap));
  1287.    if(type_of(fun)==t_vfun)
  1288.      return(fcalln_vfun(ap));
  1289.    return(fcalln_general(ap));
  1290.   va_end(ap);
  1291.  }
  1292.  
  1293. /* call a cfun eg funcall_cfun(Lmake_hash_table,2,Ktest,Seq) */
  1294. typedef void (*funcvoid)();
  1295.  
  1296. object
  1297. funcall_cfun(fn,n,va_alist)
  1298.      int n;
  1299.      funcvoid fn;
  1300.      va_dcl
  1301. {object *old_top = vs_top;
  1302.  object *old_base= vs_base;
  1303.  object result;
  1304.  va_list ap;
  1305.  DEBUG_AVMA
  1306.  vs_base=vs_top;
  1307.  va_start(ap);
  1308.  while(n-->0) vs_push(va_arg(ap,object));
  1309.  va_end(ap);
  1310.  (*fn)();
  1311.  if(vs_top>vs_base) result=vs_base[0];
  1312.  else result=Cnil;
  1313.  vs_top=old_top;
  1314.  vs_base=old_base;
  1315.  CHECK_AVMA;
  1316.  return result;}
  1317.  
  1318.  
  1319. init_eval()
  1320. {
  1321.  
  1322.         make_constant("CALL-ARGUMENTS-LIMIT", make_fixnum(64));
  1323.  
  1324.     Sapply = make_function("APPLY", Lapply);
  1325.     enter_mark_origin(&Sapply);
  1326.     Sfuncall = make_function("FUNCALL", Lfuncall);
  1327.     enter_mark_origin(&Sfuncall);
  1328.  
  1329.     Vevalhook = make_special("*EVALHOOK*", Cnil);
  1330.     Vapplyhook = make_special("*APPLYHOOK*", Cnil);
  1331.  
  1332.     temporary = Cnil;
  1333.     enter_mark_origin(&temporary);
  1334.  
  1335.     three_nils.nil3_self[0] = Cnil;
  1336.     three_nils.nil3_self[1] = Cnil;
  1337.     three_nils.nil3_self[2] = Cnil;
  1338.  
  1339.     make_function("EVAL", Leval);
  1340.     make_function("EVALHOOK", Levalhook);
  1341.     make_function("APPLYHOOK", Lapplyhook);
  1342.     siSlambda_block_expanded=make_si_special("LAMBDA-BLOCK-EXPANDED",Cnil);
  1343.     make_function("CONSTANTP", Lconstantp);
  1344.     siSbreak_points = make_si_special("*BREAK-POINTS*",Cnil);
  1345.     siSbreak_step = make_si_special("*BREAK-STEP*",Cnil);
  1346.  
  1347.     
  1348. }
  1349.